﻿
Shader "Dan/UI/UI_LoadingLine" {
    Properties {
        _Color ("Color", Color) = (0,0.4568243,1,1)
        _Color2 ("Color2", Color) = (1,0,0,1)
        _Emission ("Emission", Float ) = 1
        _Amount ("Amount", Float ) = 8
        _Width ("Width", Range(0, 1)) = 1
        _Gap ("Gap", Float ) = 0.7        
        _Percentage ("Percentage", Range(0, 1)) = 1
        _Speed ("Speed", Float ) = 2
        [MaterialToggle] _UseTime ("UseTime", Float ) = 1
        [MaterialToggle] _UseSin ("UseSin", Float ) = 0
        
        
        
    }
    SubShader {
        Tags {
            "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent"
        }
        Pass {
           
            
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            
            
            
             float4 _Color;
             float _Amount;
             float _Gap;
             float _Percentage;
             fixed _UseTime;
             float _Speed;
             fixed _UseSin;
             float _Width;
             float4 _Color2;
             float _Emission;
            
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                
                float4 vertex : SV_POSITION;
            };
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                
                return o;
            }

            float UseSinTime (float percent, float speed , float useTimeToggle, float useSinToggle ){
                float useTime = lerp( percent, frac(speed), useTimeToggle );//根据time百分比
                float useSintime =lerp( useTime, saturate(sin(speed)), useSinToggle )*1.1-0.55;//SinTime
                return useSintime ;
            }

            
            void LoadingLine (float loadingDir, float levelamount, float useSintime,
                float gapVal, float widthVal,float widthDir)
            {
                float level = floor(abs(levelamount));//分割数量
                float dir = floor((1.0 - loadingDir) * level) / (level - 1);//UV方向渐变黑白

                float gap = saturate(1- step(gapVal,frac(loadingDir*level)));//分割形状
                float width =saturate(1-step( widthVal, abs(widthDir*2-1)));//宽度
                float loadingline = step(0.5,useSintime+dir);//求增长条
                clip(loadingline*gap*width - 0.5);//剔除
            }


            fixed4 frag (v2f i ) : SV_Target {
                
                float2 uv = i.uv;
                float speed = _Time.y*_Speed;
                float useSintime = UseSinTime(_Percentage,speed,_UseTime,_UseSin);

                LoadingLine(uv.x, _Amount,useSintime,_Gap,_Width,uv.y);
                
                float3 emissive = lerp(_Color2.rgb,_Color.rgb,1-uv.x)*_Emission;
                float3 finalColor = emissive;
                return fixed4(finalColor,1);
                
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    
}
